<?php
class QuotationController extends AppController {
    var $name = 'Quotation';
    var $helpers = array();
    var $components = array('Menu','Init');
    var $uses = array('Picture'); 

    function beforeFilter() {
        $this->Init->startup($this);
        $this->login = $this->Init->init();
        $this->Init->getKeyword();
        $_session = $this -> Session -> read();
        $this->loadModel('Hitcounter');
        $row = $this->Hitcounter->get(session_id());
        if ($row == 0) {
                $data = array(
                        "session_id" => session_id(),
                        "ref_url" => env("REQUEST_URI"),
                        "ip_address" => env("REMOTE_ADDR"),
                        "activity" => date("Y-m-d H:i:s")
                );
                $this->Hitcounter->save($data);
        }
        $this->set('activeNav','Quotation');
        Configure::write("Config.language", 'vie');
        $this->set('lang','vie');
    }

    function index($id = null) {
        $this->layout = 'inside';
        // get all quotations  - kind = 6
        $result = $this->Picture->get(6);
        $this->set('result',$result); 

        if ($id) {
            if(empty($this->login['User'])) {
                $this->Session->setFlash(__('Please log in your account before.', true),'alert');
                $this->redirect(array('controller' => 'quotation','action' => 'index'));
             }
            $result = $this->Picture->findById($id);
            if ($result && $result['Picture']['kind'] == 6) {
                $filename = Router::url('/img/documents/'.$result['Picture']['image_link']);
                if (file_exists($filename)) {
                    header("Pragma: public");
                    header("Expires: 0"); 
                    header("Pragma: no-cache"); 
                    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");  
                    header("Content-Type: application/force-download"); 
                    header("Content-Type: application/octet-stream");
                    header("Content-Type: application/download");
                    header('Content-disposition: attachment; filename=' . basename($filename));
                    header("Content-Type: application/pdf");
                    header("Content-Transfer-Encoding: binary");
                    header('Content-Length: ' . filesize($filename));
                    @readfile($filename);
                }
                else {
                    $this->Session->setFlash(__('File is not existed.', true),'alert');
                }
            }
            $this->redirect(array('controller' => 'quotation','action' => 'index'));
        }
               
        $this->render('index');
    }
}
?>
